import axios from 'axios';
import query from 'query-string';

const BASE_URL = '/product/attribute/key';


export interface AttributeParam {
    name?: string;
    page?: number;
    size?: number;
    sort?: Array<string>;
}

export interface AttributeResponse {
    id?: number;
    name: string;
    unit?: string;
    standard?: number;
    type?: number;
    
    createdBy?: string;
    updatedBy?: string;
    createdAt?: string;
    updatedAt?: string;
}

export interface AttributeSaveCommand {
    id?: number;
    name: string;
    unit?: string;
    standard?: string;
    type?: string;
}

export interface AttributeResponseList {
    list: AttributeResponse[];
    total: number;
}


// 通过AttributeParam参数获取分页列表
export function pageAttribute(params: AttributeParam) {
    return axios.get<AttributeResponseList>(`${BASE_URL}/page`, {
        params, paramsSerializer: (obj) => {
            return query.stringify(obj);
        }
    })
}

// 通过id获取attribute信息
export function getAttribute(id: number) {
    return axios.get<AttributeResponse>(`${BASE_URL}/${id}`);
    
}

export function listAttribute(params: AttributeParam) {
    return axios.get<AttributeResponse[]>(`${BASE_URL}/list`, {
        params, paramsSerializer: (obj) => {
            return query.stringify(obj);
        }
    });
}

// 添加attribute
export function addAttribute(params: AttributeSaveCommand) {
    return axios.post(`${BASE_URL}/add`, params);
}

// 更新attribute
export function updateAttribute(params: AttributeSaveCommand) {
    return axios.put(`${BASE_URL}/update`, params);
}

// 批量删除attribute
export function deleteAttribute(ids: number | Array<number>) {
    return axios.delete(`${BASE_URL}/delete/${ids}`);
}